/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.session.config.base;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

/**
 * session （redis）配置基类
 */
public class RedisSessionBaseConfiguration {

    /**
     * 用于redis session，防止每次创建一个线程
     * 注：此Bean名称与 RedisHttpSessionConfiguration 中的 setRedisTaskExecutor() 要求注入的Bean名称保持一致
     */
    @Bean("springSessionRedisTaskExecutor")
    public ThreadPoolTaskExecutor springSessionRedisTaskExecutor() {
        ThreadPoolTaskExecutor sessionRedisTaskExecutor = new ThreadPoolTaskExecutor();
        sessionRedisTaskExecutor.setCorePoolSize(4);
        //默认值为Integer.MAX_VALUE
        sessionRedisTaskExecutor.setMaxPoolSize(256);
        sessionRedisTaskExecutor.setKeepAliveSeconds(10);
        sessionRedisTaskExecutor.setQueueCapacity(2048);
        sessionRedisTaskExecutor.setThreadNamePrefix("Session redis executor thread: ");
        return sessionRedisTaskExecutor;
    }

    /**
     * 解决redis集群环境没有开启Keyspace notifications导致的
     * Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource
     */
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

}
